package com.zh.note.huawei.onehundred.找终点;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author: zhanghuan
 * @date: 2023/9/2 19:02
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // String line = sc.nextLine();
        String line = "7 5 9 4 2 6 8 3 5 4 3 9";
        int[] nums = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray();
        int res = solution(nums);
        System.out.println(res);


    }

    private static int solution(int[] nums) {
        int minStepValue = Integer.MAX_VALUE;
        int cur = 0; // 当前的索引下标
        int count = 0;//统计步数
        for (int i = 2; i < nums.length / 2; i++) {
            int target = nums.length - 1; //最后位置的索引下标
            cur = i;
            count++;
            while (cur < target) {
                cur += nums[cur];
                count++;
                if (target == cur) {
                    minStepValue = Math.min(minStepValue, count);
                    break;
                }
                if (cur > target) { // 不满足题意，需要重置
                    count = 0;
                    cur = 0;
                    break;
                }
            }

        }
        if (minStepValue <= nums.length) {
            return minStepValue;
        } else {
            return -1;
        }

    }
}
